package com.hjj.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjj.springboot.entity.R;
import com.hjj.springboot.entity.UserHealthInfo;
import com.hjj.springboot.service.IUserHealthInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/my/healthData")
public class UserHealthInfoController {

    @Autowired
    private IUserHealthInfoService userHealthInfoService;

    @GetMapping("/{openid}")
    private R findUserHealthData(@PathVariable("openid") String openid){
        UserHealthInfo userHealthInfo = userHealthInfoService.getOne(new QueryWrapper<UserHealthInfo>().eq("uid", openid));
        HashMap<String, Object> map = new HashMap<>();
        map.put("userHealthInfo", userHealthInfo);
        return R.ok(map);
    }

    @PostMapping("/save")
    private R saveUserHealthData(@RequestBody UserHealthInfo userHealthInfo){
        if (userHealthInfo.getId() == null){
            userHealthInfoService.save(userHealthInfo);
        }else {
            userHealthInfoService.updateById(userHealthInfo);
        }
        return R.ok();
    }
}
